﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YAPL.Language.Core.Exceptions;
using YAPL.Language.Core.Types;

namespace YAPL.Language.Core.Lib.GlobalFunctions {
	public class ParseBool : YAPLFunction {
		private const string ARG = "arg";
		public ParseBool() : base(FunctionNames.ParseBool, GCL.Get(ClassNames.GlobalScope)) { }
		
		[Param(0,ARG)]
		public override YAPLObject Call(Scope scope) {
			ParseScope(scope);
			var arg = scope[ARG];

			if (arg is YAPLBoolean) {
				return new YAPLBoolean((arg as YAPLBoolean).BoolValue);
			}

			if (arg is YAPLInt) {
				return new YAPLBoolean((arg as YAPLInt).IntValue != 0);
			}

			throw new YAPLBadArgumentException("Couldn't parse argument", arg);
		}

		public override bool CanPrecompute {
			get {
				return true;
			}
		}
	}
}
